package com.web.security.handler;

import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liuxuhuang
 * @date 2023年04月02日 10:36
 */
@Component
public class AccessDeniedHandler implements org.springframework.security.web.access.AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType("text/json;charset=utf8");
        httpServletResponse.getWriter().println(new ObjectMapper().writeValueAsString(CommonResult.failed(ResultCode.FORBIDDEN)));
    }
}
